<template>
  <div class="Setting">
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 表头 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="isShow = true"
          >新增角色</el-button>
          <!-- table -->
          <el-table :data="tableData" border>
            <el-table-column type="index" label="序号" />
            <el-table-column prop="name" label="角色名" />
            <el-table-column prop="description" label="描述" />
            <el-table-column fixed="right" label="操作">
              <template #default="{ row }">
                <el-button
                  type="text"
                  size="small"
                  @click="handleClick(row)"
                >分配权限</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="enterEdit(row)"
                >修改</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="Confirm(row)"
                >
                  <el-button
                    slot="reference"
                    type="text"
                    size="small"
                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            :current-page="page"
            :page-sizes="[1, 2, 3]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
          <!-- 公司信息 -->
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <com-info />
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <set-dialog
      :visible.sync="isShow"
      :form="form"
      @clear="clearHandler"
      @getData="getData"
    />
  </div>
</template>

<script>
import ComInfo from './components/ComInfo.vue'
import SetDialog from './components/SetDialog.vue'
import { getRoles, getRoleById, delRoleById } from '@/api/setting'
export default {
  name: 'Setting',
  components: { ComInfo, SetDialog },

  data() {
    return {
      // 是否显示弹框
      isShow: false,
      activeName: 'second',
      page: 1,
      pagesize: 1,
      tableData: [],
      total: 0,
      form: {
        name: '',
        description: ''
      }
    }
  },
  created() {
    this.getData()
  },

  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      this.getData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getData()
    },
    async getData() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })

      // console.log(res)
      this.tableData = res.rows
      this.total = res.total
    },
    clearHandler() {
      this.form = {
        name: '',
        description: ''
      }
      this.isShow = false
    },
    async enterEdit(row) {
      const res = await getRoleById(row.id)
      // console.log(res)
      this.form = res

      this.isShow = true
    },
    async Confirm(row) {
      await delRoleById(row.id)
      // console.log(res)
      if (this.page !== 1 && this.tableData.length === 1) {
        this.page--
      }
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.Setting {
  .el-table {
    margin-top: 15px;
  }
  .el-pagination {
    text-align: right;
    margin-top: 15px;
  }
}
</style>
